<?php

namespace app\middleware;

class CheckInput
{
    public function handle($request, \Closure $next)
    {
        if(!empty($request->getInput())){
            //获取app名称
            $app_name  = $this->longbingGetAppName($request);
            //生成文件地址
            $schemaMethod =$app_name . '/schema/' .ucfirst($app_name) . ucfirst($request->controller()) . ucfirst($request->action()) . 'Request';
            //判断验证是否成功
            if (jsonSchemaExist($schemaMethod)) {
                $result = jsonSchemaValidate($schemaMethod, $request->getInput());
                if ($result) {
                    $this->_validate = true;
                } else {
                    $this->_validate = false;
                    exit();
                }
            } else {
                // 暂不处理
                todo('Schema文件找不到！');
            }
        }
        return $next($request);
    }
    
    /**
     * @author : yangqi
     * @create time : 2019年11月22日15:55:04 
     * @用途 : 通过request获取应用名称
     */
    public function longbingGetAppName($request)
    {
        $app_name = $request->root();
        return $this->longbingGetBetween($app_name ,'/');
    }
    
    public function longbingGetBetween($input, $start)
    {
        $str = substr($input, strlen($start)+strpos($input, $start),strlen($input));
        return $str;
    }
}
